﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using BCDto.OA.GeneralDatabaseOperation;
using BCService.OA.GeneralDatabasesOperation;

namespace BCTMSRESTful.Controllers.Management.OA.GeneralDatabasesOperation
{
    /// <summary>
    /// 通用数据库操作
    /// </summary>
    [ApiExplorerSettings(GroupName = "OAAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class GeneralDatabasesOperationController : ManagementBaseController
    {
        private readonly IGeneralDatabasesOperationService generalDatabasesOperationService;

        public GeneralDatabasesOperationController(IGeneralDatabasesOperationService generalDatabasesOperationService)
        {
            this.generalDatabasesOperationService = generalDatabasesOperationService;
        }
        /// <summary>
        /// 获取所有允许访问数据库名称列表
        /// </summary>      
        /// <response code="200">获取所有允许访问数据库名称列表</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(IEnumerable<GeneralDatabaseOperationDatabaseNameDto>), 200)]
        public IActionResult DatabaseName()
        {
            return Ok(this.generalDatabasesOperationService.GetAllDatabaseName());
        }

        /// <summary>
        /// 获取所有表名称列表
        /// </summary>      
        /// <param name="databaseName">数据库名称</param>
        /// <response code="200">获取所有表名称列表</response>
        /// <returns></returns>
        [HttpGet("[action]/{database_name}")]
        [ProducesResponseType(typeof(IEnumerable<GeneralDatabaseOperationTableNameDto>), 200)]
        public IActionResult TableName([FromRoute(Name = "database_name")]string databaseName)
        {
            return Ok(this.generalDatabasesOperationService.GetAllTableName(databaseName));
        }

        /// <summary>
        /// 获取所有列名称列表
        /// </summary>      
        /// <param name="databaseName">数据库名称</param>
        /// <param name="tableName">表名称</param>
        /// <response code="200">获取所有列名称列表</response>
        /// <returns></returns>
        [HttpGet("[action]/{database_name}/{table_name}")]
        [ProducesResponseType(typeof(IEnumerable<GeneralDatabaseOperationColumnNameDto>), 200)]
        public IActionResult ColumnName([FromRoute(Name = "database_name")]string databaseName,[FromRoute(Name ="table_name")]string tableName)
        {
            return Ok(this.generalDatabasesOperationService.GetAllColumnName(databaseName,tableName));
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <response code="200">获取数据</response>
        /// <returns></returns>
        [HttpGet("[action]/{db}/{table}")]
        [ProducesResponseType(typeof(IEnumerable<Object>), 200)]
        public IActionResult DataList([FromRoute(Name ="db")]string dbName, 
            [FromRoute(Name = "table")] string tableName)
        {
            var result = this.generalDatabasesOperationService.GetTableDataList(dbName,tableName);

            return Ok(result);
        }

        /// <summary>
        /// 获取数据
        /// </summary>      
        /// <param name="queryDto">查询信息</param>
        /// <response code="200">获取数据</response>
        /// <returns></returns>
        [HttpPut("[action]")]
        [ProducesResponseType(typeof(IEnumerable<Object>), 200)]
        public IActionResult QueryData([FromBody]GeneralDatabasesOperationQueryDto queryDto)
        {
            var result = this.generalDatabasesOperationService.GetTableData(queryDto);
            
            return Ok(result);
        }
    }
}
